Skip to content

Conversation

@ruudk
Copy link
Contributor

@ruudk ruudk commented Feb 3, 2021

After seeing #60 stuck I decided to give it a shot...

This is very naive, and it assumes the TreeBuilder is always of type array.

TreeBuilder::__construct(string $name, string $type = 'array', NodeBuilder $builder = null)

If I am able to find the second $type parameter to the new TreeBuilder() call then I can make the return type of getRootNode dynamic based on that.

@ondrejmirtes Is this even possible?

The issue with NodeDefinition::end() is that it can return null when you've reached the root. The current extension removes the nullability in favor of DX. But it's also very naive. No idea if that can be done dynamically.

Hope to get some advice on how to proceed.... 🙏

@ondrejmirtes
Copy link
Member

@ruudk Hi, I need a sample of analyzed code and what is this extension trying to achieve.

@ruudk
Copy link
Contributor Author

ruudk commented Feb 4, 2021

@ondrejmirtes I'm trying to let PHPStan understand this: https://github.com/symfony/symfony/blob/60118f14aa33165407b06ed59225a612049f715f/src/Symfony/Bundle/DebugBundle/DependencyInjection/Configuration.php#L30-L55

If you want to reproduce it:

git clone https://github.com/symfony/symfony.git
cd symfony
composer install
composer req --dev phpstan/phpstan:^0.12.71
vendor/bin/phpstan analyse --level=8 src/Symfony/Bundle/DebugBundle/DependencyInjection/Configuration.php

 ------ -----------------------------------------------------------------------
  Line   Configuration.php
 ------ -----------------------------------------------------------------------
  33     Call to an undefined method
         Symfony\Component\Config\Definition\Builder\NodeDefinition::children(
         ).
  58     Call to an undefined method
         Symfony\Component\Config\Definition\Builder\NodeDefinition::children(
         ).
 ------ -----------------------------------------------------------------------


 [ERROR] Found 2 errors

@ruudk ruudk changed the title Add naive extension for TreeBuilder::getRootNode() + NodeDefinition::end() Support for TreeBuilder::getRootNode() Feb 11, 2021
@ruudk
Copy link
Contributor Author

ruudk commented Feb 11, 2021

@ondrejmirtes It's green ✅ 🎉


$treeBuilder = new $className(
...$args
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should take a more "static" approach. You can also easily cause an internal error when analyzing code, for example when the constructor call has wrong arguments.

We should have some hardcoded map of what the type => root node types are. And I guess the TreeBuilderType constructor 2nd argument should be ObjectType.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the code and I feel it's now much simpler. What do you think?

I think this is how it's done everywhere.
@ondrejmirtes ondrejmirtes merged commit 6f27071 into phpstan:master Feb 11, 2021
@ondrejmirtes
Copy link
Member

Perfect, thank you!

@ruudk ruudk deleted the configuration branch February 11, 2021 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants